using Photon.Pun;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RemainChar : MonoBehaviour
{
    public ListView listView;

    public GameObject itemPrefab;
    public Transform itemRoot;
    public CharItem selectedItem;   // 当前被选中的item

    #region 事件系统
    private void Start()
    {
        this.listView.onItemSelected += this.OnCharItemSelected;
        ReFreshUI();
    }

    public void OnCharItemSelected(ListView.ListViewItem item)  // 角色卡被玩家选中时，触发该方法
    {
        this.selectedItem = item as CharItem;
        // TODO:显示当前选中角色详细信息面板


    }
    #endregion


    #region 刷新UI
    public void ReFreshUI()
    {
        ClearCharList();
        InitCharItems();
    }

    private void ClearCharList()
    {
        this.listView.RemoveAll();
    }

    private void InitCharItems()
    {
        if (PhotonNetwork.IsMasterClient)
        {
            foreach (var item in GameManager.Instance.red_CharCards)
            {
                GameObject go = Instantiate(itemPrefab, itemRoot);  // 生成预制体

                CharItem charItem = go.GetComponent<CharItem>();    // 赋值
                charItem.SetCharInfo(item);
                this.listView.AddItem(charItem);
            }
        }
        else
        {
            foreach (var item in GameManager.Instance.blue_CharCards)
            {
                GameObject go = Instantiate(itemPrefab, itemRoot);  // 生成预制体

                CharItem charItem = go.GetComponent<CharItem>();    // 赋值
                charItem.SetCharInfo(item);
                this.listView.AddItem(charItem);
            }
        }
    }
    #endregion


}
